import { defineStore } from 'pinia';
import { getUserInfo, getToken, clearAuthStorage, setToken, setUserInfo } from './helpers';

interface AuthState {
  /** 用户信息 */
  userInfo: Auth.UserInfo;
  /** 用户token */
  token: string;
}

export const useAuthStore = defineStore('auth-store', {
  state: (): AuthState => ({
    userInfo: getUserInfo(),
    token: getToken()
  }),
  getters: {
    /** 是否登录 */
    isLogin: state => Boolean(state.token)
  },
  actions: {
    /**
     * 设置auth状态
     * @param userInfo 用户信息
     */
    setAuthStore(userInfo: Auth.UserInfo) {
      this.userInfo = userInfo;
      this.token = userInfo.token!;
      setToken(this.token);
      setUserInfo(userInfo);
    },
    /** 重置auth状态 */
    resetAuthStore() {
      clearAuthStorage();
      this.$reset();
    },
    getUserInfo(): Auth.UserInfo {
      return this.userInfo;
    }
  }
});
